package thread.test_3.atomic;

import java.util.concurrent.atomic.AtomicInteger;

public class Test1 {
    public static void main(String[] args) {
        // AtomicInteger
        /*
            原子工具类：工具类设计使用的是volatile + CAS + 自旋锁
            构造方法：
                不传参数，默认值为0
                传递参数，按照int值进行初始化
         */
        AtomicInteger atom = new AtomicInteger();
        // 获取值
        System.out.println("atom.get() = " + atom.get());
        // 先获取再自增
        int andAdd = atom.getAndAdd(10);
        System.out.println("andAdd = " + andAdd);
        System.out.println("atom.get() = " + atom.get());
        // 先自增再获取
        int i = atom.addAndGet(10);
        System.out.println("i = " + i);
        System.out.println("atom.get() = " + atom.get());
        // 先获取再自增1
        int andIncrement = atom.getAndIncrement();
        System.out.println("andIncrement = " + andIncrement);
        System.out.println("atom.get() = " + atom.get());
        // 先自增1再获取
        int i1 = atom.incrementAndGet();
        System.out.println("i1 = " + i1);
        System.out.println("atom.get() = " + atom.get());
        // 先获取再自减1
        int andDecrement = atom.getAndDecrement();
        System.out.println("andDecrement = " + andDecrement);
        System.out.println("atom.get() = " + atom.get());
        // 先自减1再获取
        int i2 = atom.decrementAndGet();
        System.out.println("i2 = " + i2);
        System.out.println("atom.get() = " + atom.get());


    }
}
